package org.glow.changgou.seckill.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.glow.changgou.entity.SystemConstants;
import org.glow.changgou.seckill.dao.SeckillGoodsMapper;
import org.glow.changgou.seckill.pojo.SeckillGoods;
import org.glow.changgou.seckill.pojo.SeckillOrder;
import org.glow.changgou.seckill.service.SeckillGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/****
 * @Author:gloww
 * @Description:SeckillGoods业务层接口实现类
 *****/
@Service
public class SeckillGoodsServiceImpl extends ServiceImpl<SeckillGoodsMapper, SeckillGoods> implements SeckillGoodsService {

    @Resource
    private SeckillGoodsMapper seckillGoodsMapper;


    /**
     * SeckillGoods条件+分页查询
     *
     * @param seckillGoods 查询条件
     * @param page         页码
     * @param size         页大小
     * @return 分页结果
     */
    @Override
    public Page<SeckillGoods> findPage(SeckillGoods seckillGoods, int page, int size) {
        Page<SeckillGoods> pageInfo = new Page<>(page,size);
        LambdaQueryWrapper<SeckillGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,seckillGoods);
        return seckillGoodsMapper.selectPage(pageInfo,queryWrapper);
    }

    /**
     * SeckillGoods分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<SeckillGoods> findPage(int page, int size) {
        Page<SeckillGoods> pageInfo = new Page<>(page,size);

        return seckillGoodsMapper.selectPage(pageInfo,null);
    }

    /**
     * SeckillGoods条件查询
     *
     * @param seckillGoods
     * @return
     */
    @Override
    public List<SeckillGoods> findList(SeckillGoods seckillGoods) {
        LambdaQueryWrapper<SeckillGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,seckillGoods);
        return seckillGoodsMapper.selectList(queryWrapper);
    }


    /**
     * SeckillGoods构建查询对象
     *
     * @param seckillGoods
     * @return
     */
    public LambdaQueryWrapper<SeckillGoods> getCondition(LambdaQueryWrapper<SeckillGoods> queryWrapper, SeckillGoods seckillGoods) {

        if (seckillGoods != null) {
            // 
            if (!StringUtils.isEmpty(seckillGoods.getId())) {
//                criteria.andEqualTo("id", seckillGoods.getId());
                queryWrapper.eq(SeckillGoods::getId,seckillGoods.getId());

            }
            // spu ID
            if (!StringUtils.isEmpty(seckillGoods.getSpuId())) {
//                criteria.andEqualTo("supId", seckillGoods.getSupId());
                queryWrapper.eq(SeckillGoods::getSpuId,seckillGoods.getSpuId());
            }
            // sku ID
            if (!StringUtils.isEmpty(seckillGoods.getSkuId())) {
//                criteria.andEqualTo("skuId", seckillGoods.getSkuId());
                queryWrapper.eq(SeckillGoods::getSkuId,seckillGoods.getSkuId());
            }
            // 标题
            if (!StringUtils.isEmpty(seckillGoods.getName())) {
//                criteria.andLike("name", "%" + seckillGoods.getName() + "%");
                queryWrapper.like(SeckillGoods::getName,seckillGoods.getName());
            }
            // 商品图片
            if (!StringUtils.isEmpty(seckillGoods.getSmallPic())) {
//                criteria.andEqualTo("smallPic", seckillGoods.getSmallPic());
                queryWrapper.eq(SeckillGoods::getSmallPic,seckillGoods.getSmallPic());
            }
            // 原价格
            if (!StringUtils.isEmpty(seckillGoods.getPrice())) {
//                criteria.andEqualTo("price", seckillGoods.getPrice());
                queryWrapper.eq(SeckillGoods::getPrice,seckillGoods.getPrice());
            }
            // 秒杀价格
            if (!StringUtils.isEmpty(seckillGoods.getCostPrice())) {
//                criteria.andEqualTo("costPrice", seckillGoods.getCostPrice());
                queryWrapper.eq(SeckillGoods::getCostPrice,seckillGoods.getCostPrice());
            }
            // 添加日期
            if (!StringUtils.isEmpty(seckillGoods.getCreateTime())) {
//                criteria.andEqualTo("createTime", seckillGoods.getCreateTime());
                queryWrapper.eq(SeckillGoods::getCreateTime,seckillGoods.getCreateTime());
            }
            // 审核日期
            if (!StringUtils.isEmpty(seckillGoods.getCheckTime())) {
//                criteria.andEqualTo("checkTime", seckillGoods.getCheckTime());
                queryWrapper.eq(SeckillGoods::getCheckTime,seckillGoods.getCheckTime());
            }
            // 审核状态，0未审核，1审核通过，2审核不通过
            if (!StringUtils.isEmpty(seckillGoods.getStatus())) {
//                criteria.andEqualTo("status", seckillGoods.getStatus());
                queryWrapper.eq(SeckillGoods::getStatus,seckillGoods.getStatus());
            }
            // 开始时间
            if (!StringUtils.isEmpty(seckillGoods.getStartTime())) {
//                criteria.andEqualTo("startTime", seckillGoods.getStartTime());
                queryWrapper.eq(SeckillGoods::getStartTime,seckillGoods.getStartTime());
            }
            // 结束时间
            if (!StringUtils.isEmpty(seckillGoods.getEndTime())) {
//                criteria.andEqualTo("endTime", seckillGoods.getEndTime());
                queryWrapper.eq(SeckillGoods::getEndTime,seckillGoods.getEndTime());
            }
            // 秒杀商品数
            if (!StringUtils.isEmpty(seckillGoods.getNum())) {
//                criteria.andEqualTo("num", seckillGoods.getNum());
                queryWrapper.eq(SeckillGoods::getNum,seckillGoods.getNum());
            }
            // 剩余库存数
            if (!StringUtils.isEmpty(seckillGoods.getStockCount())) {
//                criteria.andEqualTo("stockCount", seckillGoods.getStockCount());
                queryWrapper.eq(SeckillGoods::getStockCount,seckillGoods.getStockCount());
            }
            // 描述
            if (!StringUtils.isEmpty(seckillGoods.getIntroduction())) {
//                criteria.andEqualTo("introduction", seckillGoods.getIntroduction());
                queryWrapper.eq(SeckillGoods::getIntroduction,seckillGoods.getIntroduction());
            }
        }
        return queryWrapper;
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        seckillGoodsMapper.deleteById(id);
    }

    /**
     * 修改SeckillGoods
     *
     * @param seckillGoods
     */
    @Override
    public void update(SeckillGoods seckillGoods) {
        seckillGoodsMapper.updateById(seckillGoods);
    }

    /**
     * 增加SeckillGoods
     *
     * @param seckillGoods
     */
    @Override
    public void add(SeckillGoods seckillGoods) {
        seckillGoodsMapper.insert(seckillGoods);
    }

    /**
     * 根据ID查询SeckillGoods
     *
     * @param id
     * @return
     */
    @Override
    public SeckillGoods findById(Long id) {
        return seckillGoodsMapper.selectById(id);
    }

    /**
     * 查询SeckillGoods全部数据
     *
     * @return
     */
    @Override
    public List<SeckillGoods> findAll() {
        return seckillGoodsMapper.selectList(null);
    }

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public List<SeckillGoods> list(String key) {
        return redisTemplate.boundHashOps("SeckillGoods_"+key).values();
    }

    @Override
    public SeckillGoods one(String time, Long id) {
        return (SeckillGoods) redisTemplate.boundHashOps(SystemConstants.SEC_KILL_GOODS_PREFIX + time).get(id);
    }


}
